package com.eipc.util;

import com.eipc.model.Department;
import com.eipc.model.ImportNotice;
import com.eipc.model.ImportNoticeChild;
import com.eipc.model.Position;
import com.eipc.model.Reply;
import com.eipc.model.Staff;
import com.eipc.model.Version;

public class DataParse {
	public static final String PREX = ";;";

	// serverid,appType,{content},{imageWebUrl},issuer,issueTime,sender,sendTime,sendType,top
	public static ImportNotice ParseImportNotice(String strImportNotice) {
		try {
			String[] myString = strImportNotice.split(PREX);
			if (myString.length == 13) {
				ImportNotice myImportNotice = new ImportNotice(Integer
						.valueOf(myString[0]), Integer.valueOf(myString[1]),
						myString[2].substring(1, myString[2].length() - 1),
						myString[3].substring(1, myString[3].length() - 1), "",
						myString[4], myString[5], myString[6], myString[7],
						myString[10], myString[8], myString[11], myString[12],
						myString[9]);
				return myImportNotice;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	

	public static String formatImportNotice(ImportNotice in) {
		if (in != null) {
			StringBuffer sb = new StringBuffer();
			sb.append(in.id);
			sb.append(PREX + getValue(in.appType));
			sb.append(PREX + "{" + getValue(in.content + "}"));
			sb.append(PREX + "{" + getValue(in.imageWebUrl + "}"));
			sb.append(PREX + getValue(in.issuer));
			sb.append(PREX + getValue(in.issueTime));
			sb.append(PREX + getValue(in.sender));
			sb.append(PREX + getValue(in.sendTime));
			sb.append(PREX + getValue(in.sendType));
			sb.append(PREX + getValue(in.top));
			return sb.toString();
		} else {
			return null;
		}
	}

	public static Reply ParseReply(String strConsult) {
		try {
			String[] myString = strConsult.split(PREX);
			if (myString.length == 3) {
				Reply r = new Reply("", myString[0], myString[1], myString[2]);
				return r;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// int id,int appType,String receiverMobile,String isRead,String
	// replyContent,
	// String replyTime
	public static ImportNoticeChild ParseImportNoticeChild(
			String strImportNoticeChild) {
		try {
			String[] myString = strImportNoticeChild.split(PREX);
			if (myString.length == 5) {
				ImportNoticeChild myImportNoticeChild = new ImportNoticeChild(
						Integer.valueOf(myString[0]), myString[1], myString[2],
						myString[3], myString[4]);
				return myImportNoticeChild;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// int id,int appType,String receiverMobile,String isRead,String
	// replyContent,
	// String replyTime
	public static String formatImportNoticeChild(ImportNoticeChild inc) {
		if (inc != null) {
			StringBuffer sb = new StringBuffer();
			sb.append(inc.id);
			sb.append(PREX + getValue(inc.appType));
			sb.append(PREX + getValue(inc.receiverMobile));
			sb.append(PREX + getValue(inc.isRead));
			sb.append(PREX + getValue(inc.replyContent));
			sb.append(PREX + getValue(inc.replyTime));
			return sb.toString();
		} else {
			return null;
		}
	}

	public static String getValue(Object v) {
		if (v == null) {
			return "";
		} else {
			return String.valueOf(v);
		}
	}

	public static void main(String[] args) {
		String xx = "55;;0;;{算法的放大 <br>};;{};;1;;2010-09-16 12:00;;1;;2010-09-16 12:00;;天气预报;;1";
		ParseImportNotice(xx);
	}

	public static Version ParseVersion(String strApp) {
		try {
			String[] myString = strApp.split(PREX);
			if (myString.length == 3) {
				Version app = new Version(Integer.parseInt(myString[0]),
						Integer.parseInt(myString[1]), Integer
								.parseInt(myString[2]));
				return app;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Staff ParseStaff(String strApp) {
		try {
			String[] myString = strApp.split(PREX);
			if (myString.length == 7) {
				Staff app = new Staff(myString[0], myString[1], myString[2],
						myString[3], myString[4], myString[5], myString[6]);

				return app;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Department ParseDepartment(String strApp) {
		try {
			String[] myString = strApp.split(PREX);
			if (myString.length == 2) {
				Department app = new Department(myString[0], myString[1]);
				return app;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Position ParseWorkgroup(String strApp) {
		try {
			String[] myString = strApp.split(PREX);
			if (myString.length == 2) {
				Position app = new Position(myString[0], myString[1]);
				return app;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}